*===============================================================================
* Project title		Savings Accounts to Borrow Less	
* PIs			Felipe Kast, Dina Pomeranz 
* File name		1_tables_balance.do
*-------------------------------------------------------------------------------
* Description		Creates balance, take-up, and summary statistics tables 
* Outputs		Tables 1, 7, A2, A3, A20
* Spaces per tab 	8 
*===============================================================================

*===============================================================================
*TOC
*1)	Prepare data for analysis
*2)	Tables
*2.1)	Balance table
*2.2)	Summary statistics table
*2.3)	Take-up characteristics table
*2.4)	Demand effects table
*2.5)	Summary statistics for short-term debt at baseline table
*===============================================================================

*===============================================================================
* Section 1 - Prepare data for analysis
*===============================================================================

clear all
global path "~/file_server/project_savings/2_shared/impact/do/November2019"	// Path to processed data 
global results "$path/results"		
cd "$results"									// Change to directory to store results 
set more off

*Loading processed data
	use $path/datasets/impactDatabase.dta, clear

*===============================================================================
* Section 2 - Tables 
*===============================================================================

*-------------------------------------------------------------------------------
* 2.1. Balance Table
*-------------------------------------------------------------------------------
* 2.1.a. Calculates table content ---------------------------------------------
preserve
	keep if post == 0 //drops 2009 to record baseline values
	
	//calculates content from row 3 to row 35, except for median diffs
	local i = 3
	local j = `i' + 1
	
	// iterate over balance variables
	foreach x in age educationYears householdSize incomePerCapita 		///
		hasPrevAccount allSavingsAd w5_allSavingsAd 			///
		savingsCatAd shortTermBorrowing 				///
		w5_shortTermBorrowing stBorrowTotalIndex totalLending 		///
		w5_totalLending totalLendingIndex consumptionCutbacks  		///
		anxietyFutureAnswer economicDifficultyAnswer regretsNotSaving 	///
		sociallyTaxed conflictsMoney economicShock2008 {
	
		//column (1)
		sum `x' if accountAccess == 0
		local c2r`i' = `r(mean)' 
		local c2r`j' = `r(sd)' 
		
		//column (2)
		reg `x' accountAccess, vce(cluster groupId)
		local c3r`i' = _b[accountAccess]  
		local c3r`j' = _se[accountAccess]
		test accountAccess = 0
		// stores stars for coefficient
		if (`r(p)' <= 0.1 & `r(p)' > 0.05 ) { 
			local c3r`i'coef "*"
		}
		else if (`r(p)' <= 0.05 & `r(p)' > 0.01 ) { 
			local c3r`i'coef "**"
		}
		else if (`r(p)' <= 0.01 ) { 
			local c3r`i'coef "***"
		}
		else {
			local c3r`i'coef ""
		}


		//column (3)
		sum `x' if accountAccess == 0 & inSample == 1
		local c4r`i' = `r(mean)'  
		local c4r`j' = `r(sd)'
		
		//column (4)
		reg `x' accountAccess if inSample == 1, vce(cluster groupId)
		local c5r`i' = _b[accountAccess]  
		local c5r`j' = _se[accountAccess]
		test accountAccess = 0
		// stores stars for coefficient
		if (`r(p)' <= 0.1 & `r(p)' > 0.05 ) { 
			local c5r`i'coef "*"
		}
		else if (`r(p)' <= 0.05 & `r(p)' > 0.01 ) { 
			local c5r`i'coef "**"
		}
		else if (`r(p)' <= 0.01 ) { 
			local c5r`i'coef "***"
		}
		else {
			local c5r`i'coef ""
		}	
		//column (5)
		sum `x' if inSample == 1
		local c6r`i' = `r(mean)'  
		local c6r`j' = `r(sd)'
		
		//column (6)
		reg `x' attritor, vce(cluster groupId)
		local c7r`i' = _b[attritor]  
		local c7r`j' = _se[attritor]	
		test attritor = 0
		// stores stars for coefficient
		if (`r(p)' <= 0.1 & `r(p)' > 0.05 ) { 
			local c7r`i'coef "*"
		}
		else if (`r(p)' <= 0.05 & `r(p)' > 0.01 ) { 
			local c7r`i'coef "**"
		}
		else if (`r(p)' <= 0.01 ) { 
			local c7r`i'coef "***"
		}
		else {
			local c7r`i'coef ""
		}
		
		local i = `j' + 1
		local j = `i' + 1	
	}

	//calculates content of median diff rows
	local i = 45
	local j = 455
	
	foreach x in incomePerCapita allSavingsAd shortTermBorrowing ///
		totalLending {
	
		//column (1)
		sum `x' if accountAccess == 0, detail
		local c2r`i' = trim("`: display %12.0fc r(p50)'")  
		
		//column (2)
		qreg `x' accountAccess
		local c3r`i' = trim("`: display %12.0fc _b[accountAccess]'") 
		local c3r`j' = trim("`: display %12.0fc _se[accountAccess]'") 
		test accountAccess = 0
		// stores stars for coefficient
		if (`r(p)' <= 0.1 & `r(p)' > 0.05 ) { 
			local c3r`i'coef "*"
		}
		else if (`r(p)' <= 0.05 & `r(p)' > 0.01 ) { 
			local c3r`i'coef "**"
		}
		else if (`r(p)' <= 0.01 ) { 
			local c3r`i'coef "***"
		}
		else {
			local c3r`i'coef ""
		}
		
		//column (3)
		sum `x' if accountAccess == 0 & inSample == 1, detail
		local c4r`i' = trim("`: display %12.0fc r(p50)'")   
		
		//column (4)
		qreg `x' accountAccess if inSample == 1
		local c5r`i' = trim("`: display %12.0fc _b[accountAccess]'")
		local c5r`j' = trim("`: display %12.0fc _se[accountAccess]'")   
		test accountAccess = 0
		// stores stars for coefficient
		if (`r(p)' <= 0.1 & `r(p)' > 0.05 ) { 
			local c5r`i'coef "*"
		}
		else if (`r(p)' <= 0.05 & `r(p)' > 0.01 ) { 
			local c5r`i'coef "**"
		}
		else if (`r(p)' <= 0.01 ) { 
			local c5r`i'coef "***"
		}
		else {
			local c5r`i'coef ""
		}
		
		//column (5)
		sum `x' if inSample == 1, detail
		local c6r`i' = trim("`: display %12.0fc r(p50)'")    
		
		//column (6)
		qreg `x' attritor 
		local c7r`i' = trim("`: display %12.0fc _b[attritor]'")  
		local c7r`j' = trim("`: display %12.0fc _se[attritor]'")	
		test attritor = 0
		// stores stars for coefficient
		if (`r(p)' <= 0.1 & `r(p)' > 0.05 ) { 
			local c7r`i'coef "*"
		}
		else if (`r(p)' <= 0.05 & `r(p)' > 0.01 ) { 
			local c7r`i'coef "**"
		}
		else if (`r(p)' <= 0.01 ) { 
			local c7r`i'coef "***"
		}
		else {
			local c7r`i'coef ""
		}
		
		local i = `i' + 1
		local j = `j' + 10	
	}
	
	//formats values for proper display
	forvalues i = 3/44 {
		forvalues j = 2/7 {
			if (abs(`c`j'r`i'') > 100) {
				local c`j'r`i' = trim("`: display %12.0fc `c`j'r`i'''"+"`c`j'r`i'coef'")
				di "`c`j'r`i''"
				
			} 
			else {
				local c`j'r`i' = trim("`: display %12.2fc `c`j'r`i'''"+"`c`j'r`i'coef'")
				di "`c`j'r`i''" 
			}
		}
	}
	
	//special cells, row 49
	count if accountAccess == 0
	local c2r49 = trim("`: display %12.0fc r(N)'")
	count if accountAccess != .
	local c3r49 = trim("`: display %12.0fc r(N)'")
	count if accountAccess == 0 & inSample == 1  
	local c4r49 = trim("`: display %12.0fc r(N)'")
	count if inSample == 1  
	local c5r49 = trim("`: display %12.0fc r(N)'")
	count if inSample==1
	local c6r49 = trim("`: display %12.0fc r(N)'")
	count if attritor!=. & post == 0
	local c7r49 = trim("`: display %12.0fc r(N)'")
	
	//special cells
	gen attrition = (inSample != 1)
	sum attrition if accountAccess == 0
	local c2r1 = trim("`: display %12.2fc r(mean)'") 
	local c2r2 = trim("`: display %12.2fc r(sd)'") 
	
	reg attrition accountAccess, vce(cluster groupId)
	local c3r1 = trim("`: display %12.2fc _b[accountAccess]'") 
	local c3r2 = trim("`: display %12.2fc _se[accountAccess]'") 
	test accountAccess = 0
	// stores stars for coefficient
	if (`r(p)' <= 0.1 & `r(p)' > 0.05 ) { 
		local c3r1coef "*"
	}
	else if (`r(p)' <= 0.05 & `r(p)' > 0.01 ) { 
		local c3r1coef "**"
	}
	else if (`r(p)' <= 0.01 ) { 
		local c3r1coef "***"
	}
	else {
		local c3r1coef ""
	}
	
	
	* F-test for attrition 
	local test_vars age educationYears householdSize incomePerCapita 	///
	hasPrevAccount allSavingsAd w5_allSavingsAd savingsCatAd 		///
	shortTermBorrowing w5_shortTermBorrowing stBorrowTotalIndex 		///
	totalLending w5_totalLending totalLendingIndex consumptionCutbacks 	///
	anxietyFutureAnswer economicDifficultyAnswer regretsNotSaving 		///
	sociallyTaxed conflictsMoney economicShock2008 
		
	foreach Y in `test_vars' { 
		local Ytest = "`Ytest' [`Y'_mean] attritor" 
	}	
		
	foreach x in `test_vars' {
		reg `x' attritor
		estimates store `x'
	}
	
	suest `test_vars', vce(cluster groupId)
	test `Ytest'
	local c7Fstat: di %12.2fc r(chi2)/r(df) 				// F-stat can be computed as F = chi2 / degrees of freedom. 
										// See http://www.stata.com/support/faqs/statistics/chi-squared-and-f-distributions/
	local c7Pval: di %12.3fc r(p)
	local c7Pval: di trim("`c7Pval'") 
	
	di "`c7Fstat' (`c7Pval')"
	
restore


* 2.1.b. Writes table ---------------------------------------------------------

	file open table using table1.tex, write text replace
	
	// writes table's header
	file write table "\begin{tabular}{l c c c c c c}" _n ///
		"\hline \hline" _n ///
		" & \multicolumn{2}{c}{Full Sample} & " ///
		"\multicolumn{2}{c}{Estimation Sample} & " ///
		"\multicolumn{2}{c}{Attritors vs. Non-Attritors} \\" ///
		" & & & \multicolumn{2}{c}{(Excluding Attritors)} & & \\" ///
		"\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7}" _n ///
		" & (1) & (2) & (3) & (4) & (5) & (6) \\" _n ///
                " & Control & Difference: & Control & Difference: & Non-Attritors & Difference: \\" _n ///
                " & group & treatment - control  & group & treatment - control & & attritors - non-attritors \\" _n ///
		"\hline" _n
	
	file write table ///
		"Age & `c2r3' & `c3r3' & `c4r3' & `c5r3' & `c6r3' & `c7r3' \\" _n ///
		" & (`c2r4') & (`c3r4') & (`c4r4') & (`c5r4') & (`c6r4') & (`c7r4') \\" _n ///
		"Years of education & `c2r5' & `c3r5' & `c4r5' & `c5r5' & `c6r5' & `c7r5' \\" _n ///
		" & (`c2r6') & (`c3r6') & (`c4r6') & (`c5r6') & (`c6r6') & (`c7r6') \\" _n ///
		"Household size & `c2r7' & `c3r7' & `c4r7' & `c5r7' & `c6r7' & `c7r7' \\" _n ///
		" & (`c2r8') & (`c3r8') & (`c4r8') & (`c5r8') & (`c6r8') & (`c7r8') \\" _n ///
		"Per capita monthly & `c2r9' & `c3r9' & `c4r9' & `c5r9' & `c6r9' & `c7r9' \\" _n ///
		"household income &(`c2r10') & (`c3r10') & (`c4r10') & (`c5r10') & (`c6r10') & (`c7r10') \\" _n ///
		" & [`c2r45'] & [`c3r45'] & [`c4r45'] & [`c5r45'] & [`c6r45'] & [`c7r45'] \\" _n ///
		"Has prior savings account & `c2r11' & `c3r11' & `c4r11' & `c5r11' & `c6r11' & `c7r11' \\" _n ///
		" & (`c2r12') & (`c3r12') & (`c4r12') & (`c5r12') & (`c6r12') & (`c7r12') \\" _n ///
		"Total financial savings amount & `c2r13' & `c3r13' & `c4r13' & `c5r13' & `c6r13' & `c7r13' \\" _n ///
		" & (`c2r14') & (`c3r14') & (`c4r14') & (`c5r14') & (`c6r14') & (`c7r14') \\" _n ///
		" & [`c2r46'] & [`c3r46'] & [`c4r46'] & [`c5r46'] & [`c6r46'] & [`c7r46'] \\" _n ///
		"Total financial savings amount & `c2r15' & `c3r15' & `c4r15' & `c5r15' & `c6r15' & `c7r15' \\" _n ///
		"(winsorized at the top and & (`c2r16') & (`c3r16') & (`c4r16') & (`c5r16') & (`c6r16') & (`c7r16') \\" _n ///
		"bottom 5\%) & & & & & & \\" _n ///
		"Total financial savings  & `c2r17' & `c3r17' & `c4r17' & `c5r17' & `c6r17' & `c7r17' \\" _n ///
		"categories & (`c2r18') & (`c3r18') & (`c4r18') & (`c5r18') & (`c6r18') & (`c7r18') \\" _n ///
		"Short-term borrowing amount & `c2r19' & `c3r19' & `c4r19' & `c5r19' & `c6r19' & `c7r19' \\" _n ///
		"& (`c2r20') & (`c3r20') & (`c4r20') & (`c5r20') & (`c6r20') & (`c7r20') \\" _n ///
		" & [`c2r47'] & [`c3r47'] & [`c4r47'] & [`c5r47'] & [`c6r47'] & [`c7r47'] \\" _n ///
		"Short-term borrowing amount & `c2r21' & `c3r21' & `c4r21' & `c5r21' & `c6r21' & `c7r21' \\" _n ///
		"(winsorized at the top 5\%) & (`c2r22') & (`c3r22') & (`c4r22') & (`c5r22') & (`c6r22') & (`c7r22') \\" _n ///
		"Short-term borrowing & `c2r23' & `c3r23' & `c4r23' & `c5r23' & `c6r23' & `c7r23' \\" _n ///
		"categories & (`c2r24') & (`c3r24') & (`c4r24') & (`c5r24') & (`c6r24') & (`c7r24') \\" _n ///
		"Short-term lending amount & `c2r25' & `c3r25' & `c4r25' & `c5r25' & `c6r25' & `c7r25' \\" _n ///
		" & (`c2r26') & (`c3r26') & (`c4r26') & (`c5r26') & (`c6r26') & (`c7r26') \\" _n ///
		" & [`c2r48'] & [`c3r48'] & [`c4r48'] & [`c5r48'] & [`c6r48'] & [`c7r48'] \\" _n ///
		"Short-term lending amount & `c2r27' & `c3r27' & `c4r27' & `c5r27' & `c6r27' & `c7r27' \\" _n ///
		"(winsorized at the top 5\%) & (`c2r28') & (`c3r28') & (`c4r28') & (`c5r28') & (`c6r28') & (`c7r28') \\" _n ///
		"Short-term lending categories  & `c2r29' & `c3r29' & `c4r29' & `c5r29' & `c6r29' & `c7r29' \\" _n ///
		" & (`c2r30') & (`c3r30') & (`c4r30') & (`c5r30') & (`c6r30') & (`c7r30') \\" _n ///
		"Need to cut back consumption & `c2r31' & `c3r31' & `c4r31' & `c5r31' & `c6r31' & `c7r31' \\" _n ///
		" & (`c2r32') & (`c3r32') & (`c4r32') & (`c5r32') & (`c6r32') & (`c7r32') \\" _n ///
		"Anxious about financial future & `c2r33' & `c3r33' & `c4r33' & `c5r33' & `c6r33' & `c7r33' \\" _n ///
		" & (`c2r34') & (`c3r34') & (`c4r34') & (`c5r34') & (`c6r34') & (`c7r34') \\" _n ///
		"Recent economic difficulty & `c2r35' & `c3r35' & `c4r35' & `c5r35' & `c6r35' & `c7r35' \\" _n ///
		" & (`c2r36') & (`c3r36') & (`c4r36') & (`c5r36') & (`c6r36') & (`c7r36') \\" _n ///
		"Regretted not saving more & `c2r37' & `c3r37' & `c4r37' & `c5r37' & `c6r37' & `c7r37' \\" _n ///
		" & (`c2r38') & (`c3r38') & (`c4r38') & (`c5r38') & (`c6r38') & (`c7r38') \\" _n ///
		"Socially taxed & `c2r39' & `c3r39' & `c4r39' & `c5r39' & `c6r39' & `c7r39' \\" _n ///
		" & (`c2r40') & (`c3r40') & (`c4r40') & (`c5r40') & (`c6r40') & (`c7r40') \\" _n ///
		"Household conflicts over money & `c2r41' & `c3r41' & `c4r41' & `c5r41' & `c6r41' & `c7r41' \\" _n ///
		" & (`c2r42') & (`c3r42') & (`c4r42') & (`c5r42') & (`c6r42') & (`c7r42') \\" _n ///
		"Economic shock & `c2r43' & `c3r43' & `c4r43' & `c5r43' & `c6r43' & `c7r43' \\" _n ///
		" & (`c2r44') & (`c3r44') & (`c4r44') & (`c5r44') & (`c6r44') & (`c7r44') \\" _n ///
		"Attrition & `c2r1' & `c3r1'`c3r1coef' & & & & \\" _n ///
		"& (`c2r2') & (`c3r2') & & & &  \\  \hline" _n ///
		"Number of individuals & `c2r49' & `c3r49' & `c4r49' & `c5r49' & `c6r49' & `c7r49' \\" _n
		
		
	// writes table's closing argument
	file write table "\hline \hline" _n "\end{tabular}"
	
	file close table	


*-------------------------------------------------------------------------------
* 2.2. Descriptive Take-Up stats
*-------------------------------------------------------------------------------
* 2.2.a. Calculates table content ---------------------------------------------
preserve
	keep if year == 2008 // keep only baseline year to avoid counting twice

	count if inSample == 1 & accountAccess == 1
	local tn = `r(N)'
	local c3r1 = trim("`: display %12.0fc r(N)'")  
	local c3r2 = trim("`: display %12.0fc r(N)'")  
	
	count if openedAccount == 1 & inSample == 1
	local c2r1 = trim("`: display %12.0fc r(N)'")  
	local c4r1 = trim("`: display %12.0fc 100 * r(N) / `tn''\%")
	
	count if activeUser == 1 & inSample == 1				
	local c2r2 = trim("`: display %12.0fc r(N)'") 
	local c4r2 = trim("`: display %12.0fc 100 * r(N) / `tn''\%")
	
	local i = 3
	foreach x in numDepositsJul08Aug09 totalDepositedJul08Aug09 ///
		numWithdrawalsJul08Aug09 totalWithdrawalJul08Aug09 ///
		avgMonthSavingsAug08Aug09 {
		
		sum `x' if activeUser == 1 & inSample == 1, d
		
		if ("`x'" == "numDepositsJul08Aug09" | ///
			"`x'" == "numWithdrawalsAug08Aug09") { 
		
			local c2r`i' = trim("`: display %12.1fc r(mean)'") 
			local c3r`i' = trim("`: display %12.1fc r(p50)'") 
			local c4r`i' = trim("`: display %12.1fc r(sd)'") 
		} 
		else {
			local c2r`i' = trim("`: display %12.0fc r(mean)'") 
			local c3r`i' = trim("`: display %12.0fc r(p50)'") 
			local c4r`i' = trim("`: display %12.0fc r(sd)'") 
		
		}
		
		local i = `i' + 1
	
	}
	
* II.2.b. Writes the table -----------------------------------------------------

	file open table using table2.tex, write text replace
	
	// writes table's header
	file write table "\begin{tabular}{l c c c}" _n ///
		"\hline \hline" _n ///
		"\multicolumn{4}{l}{\textbf{Panel A:} Take-Up Statistics} \\" _n ///
		"\hline" _n ///
		" & Number of & Total & Percent \\" _n ///
		" & individuals & sample & sample \\" _n ///
		"\hline" _n
		
	file write table ///
		"Opened account & `c2r1' & `c3r1' & `c4r1' \\" _n ///
		"Active user & `c2r2' & `c3r2' & `c4r2' \\" _n ///

	file write table "\hline \hline" _n ///
		"\multicolumn{4}{l}{\textbf{Panel B:} Account Usage Conditional on Being an Active User} \\" _n ///
		"\hline" _n ///
		" & Mean & Median & Std. Dev. \\" _n ///
		"\hline" _n	

	file write table ///
		"Number of deposits & `c2r3' & `c3r3' & `c4r3' \\" _n ///
		"Amount deposited & `c2r4' & `c3r4' & `c4r4' \\" _n ///
		"Number of withdrawals & `c2r5' & `c3r5' & `c4r5' \\" _n ///
		"Amount withdrawn & `c2r6' & `c3r6' & `c4r6' \\" _n ///
		"Average end of month balance & `c2r7' & `c3r7' & `c4r7' \\" _n 
			
	// writes table's closing argument
	file write table "\hline \hline" _n "\end{tabular}"
	
	file close table
restore

*-------------------------------------------------------------------------------
* 2.3. Take-Up 							
*-------------------------------------------------------------------------------
* 2.3.1. Calculates table content ---------------------------------------------
	
	eststo clear
	
	//take up over demographic variables (column 1)
	reg activeUser female age c.age#c.age educationYears childrenAtHome ///
		lnIncomePerCap hasPrevAccount if post == 0 & ///
		inSample == 1 & accountAccess == 1, vce(cluster groupId) 
	
	
	
	sum activeUser if e(sample) == 1 // control's mean outcome
	if (abs(`r(mean)') < 100) {
		estadd local control_mean = trim("`: display %12.3fc r(mean)'")
	} 
	else {
		estadd local control_mean = trim("`: display %12.0fc r(mean)'")
	}
	
	eststo
	
	//take up over other characteristics (column 2 & 3)			
	reg activeUser female age c.age#c.age educationYears childrenAtHome ///
		lnIncomePerCap hasPrevAccount headHousehold ///
		conflictsMoney sociallyTaxed regretsNotSaving ///
		hyperbolicPreferences fearStolen if post == 0 & ///
		inSample == 1 & accountAccess == 1, vce(cluster groupId) 
	
	sum activeUser if e(sample) == 1 // control's mean outcome
	if (abs(`r(mean)') < 100) {
		estadd local control_mean = trim("`: display %12.3fc r(mean)'")
	} 
	else {
		estadd local control_mean = trim("`: display %12.0fc r(mean)'")
	}
	
	eststo	
	
	reg activeUser female age c.age#c.age educationYears childrenAtHome ///
		lnIncomePerCap hasPrevAccount headHousehold ///
		conflictsMoney lentFamily owesFamily regretsNotSaving ///
		hyperbolicPreferences fearStolen if post == 0 & ///
		inSample == 1 & accountAccess == 1, vce(cluster groupId) 	
		

	sum activeUser if e(sample) == 1 // control's mean outcome
	if (abs(`r(mean)') < 100) {
		estadd local control_mean = trim("`: display %12.3fc r(mean)'")
	} 
	else {
		estadd local control_mean = trim("`: display %12.0fc r(mean)'")
	}
	
	eststo
	
* 2.3.2. Writes table ---------------------------------------------------------		

	esttab est* using tab_takeup.tex, replace 				///
		cells(b(star fmt(3)) se(par fmt(3))) label style(tex) 		///
		stats(control_mean r2 N , fmt(%12.0fc %12.3fc %12.0fc) 		///
			label("Mean take-up" "R-squared" "Observations")) 	///
		varlabels(_cons "Constant" c.age#c.age "Age$^2$") 		///
		mlabels("Take-up" "Take-up" "Take-up") collabels(, none)	///
		starlevels(* 0.1 ** 0.05 *** 0.01) eqlabels(, none) 		///
		prehead(\begin{tabular}{l*{@M}{c}} \hline \hline)  		///
		posthead(\hline ) 						///
		prefoot(\hline) postfoot(\hline \hline \end{tabular}) 


*-------------------------------------------------------------------------------
* 2.4. Demand Effects						
*-------------------------------------------------------------------------------
* 2.4.1. Calculates table content ---------------------------------------------
	
	eststo clear
	
	foreach y in difficultySurvey satisfactionFE {
		reghdfe `y' accountAccess if post == 1, 			///
		abs(asesor2008) vce(cluster groupId) 
		estadd local ind_FE = "No"
		estadd local str_FE = "Yes"
		eststo
	}
	
	
* 2.4.2. Writes table ---------------------------------------------------------	
	
	esttab est* using tab_demandeffects_FE.tex, replace				///
		cells(b(star fmt(3)) se(par fmt(3))) label style(tex) 			///
		stats(N ind_FE str_FE, fmt(%12.0fc) label("Observations" 		///
		"Individual FE" "Stratum FE $\times$ post"	)) 			///
		keep(accountAccess)							///
		varlabels(accountAccess "Account") 					///
		mlabels(, none) nonumber starlevels(* 0.1 ** 0.05 *** 0.01) 		///
		collabels(, none) eqlabels(, none) 					///
		prehead(\begin{tabular}{l*{@M}{c}} \hline \hline 			///
			& (1) & (2) \\ 							///
			& Difficulty of & Satisfaction \\ 				///
			& survey process & with FE \\)  				///
		posthead(\hline ) 							///
		prefoot(\hline) postfoot(\hline \hline \end{tabular}) 



*-------------------------------------------------------------------------------
* 2.5. Summary stats of short-term debt
*-------------------------------------------------------------------------------
* 2.5.1. Calculates table content ---------------------------------------------
preserve
	keep if inSample==1 & post==0

	local i = 4

	foreach x in iBorrowServProv iBorrowSchools iBorrowRegularClient ///
		iBorrowParents iBorrowFinancieras iBorrowSuppliers ///
		iBorrowFriends iBorrowSiblings iBorrowRelatives ///  
		iBorrowSonsDaughters iBorrowPartner iBorrowHealth ///
		iBorrowCooperatives iBorrowMoneyLenders iBorrowBusContacts {
	
		//column (1)
		sum `x' 
		local pct = 100*r(mean)
		di `pct'
		local c2r`i' = trim("`: display %12.2fc `pct''\%") 
		
		local i = `i' + 1
		}
	
	local i = 4
	
	foreach x in borrowServProv borrowSchools borrowRegularClient ///
		borrowParents borrowFinancieras borrowSuppliers ///
		borrowFriends borrowSiblings borrowRelatives ///  
		borrowSonsDaughters borrowPartner borrowHealth ///
		borrowCooperatives borrowMoneyLenders borrowBusContacts {
	
		//column (2)
		sum `x'  
		local sum`x' = r(sum)
		sum shortTermBorrowing
		local sumSTBorr = r(sum)
		local pct = 100*(`sum`x'')/(`sumSTBorr')
		di `pct'
		local c3r`i' = trim("`: display %12.2fc `pct''\%") 
		
		local i = `i' + 1
		}
		
* 2.5.2. Writes table ---------------------------------------------------------

	file open table using tab_SumStatsStDebt.tex, write text replace
	
	// writes table's header
	file write table "\begin{tabular}{l c c}" _n ///
		"\hline \hline" _n ///
		" & (1) & (2) \\" _n ///
                " & Percent of & Percent of \\" _n ///
                " & individuals & total \\" _n ///" & Percent of & Difference: \\" _n ///
                " & who have & short-term \\" _n ///
		" & this type & debt \\" _n ///
		" & of debt &  \\" _n ///
		"\hline" _n
	
	file write table ///
		"Service providers & `c2r4' & `c3r4' \\" _n ///
		"Schools & `c2r5' & `c3r5' \\" _n ///
		"Regular clients & `c2r6' & `c3r6' \\" _n ///
		"Parents & `c2r7' & `c3r7'  \\" _n ///
		"Financieras & `c2r8' & `c3r8' \\" _n ///
		"Suppliers  & `c2r9' & `c3r9' \\" _n ///
		"Friends & `c2r10' & `c3r10' \\" _n ///
		"Siblings & `c2r11' & `c3r11' \\" _n ///
		"Relatives & `c2r12' & `c3r12' \\" _n ///
		"Sons and daughters & `c2r13' & `c3r13' \\" _n ///
		"Partner & `c2r14' & `c3r14' \\" _n ///
		"Doctors and health institutions & `c2r15' & `c3r15' \\" _n ///
		"Cooperatives & `c2r16' & `c3r16' \\" _n ///
		"Money lenders & `c2r17' & `c3r17' \\" _n ///
		"Business contacts & `c2r18' & `c3r18' \\" _n ///
		
	// writes table's closing argument
	file write table "\hline \hline" _n "\end{tabular}"
	
	file close table

restore	
